package com.ruoyi.web.controller.saas;

import com.bonuo.saas.domain.ResearchProjectQuery;
import com.bonuo.saas.service.ResearchProjectQueryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/saas/researchProjectQuery")
@Api(tags = "网站查询信息")
public class ResearchProjectQueryController extends BaseController {

    @Autowired
    private ResearchProjectQueryService researchProjectQueryService;

    @ApiOperation(value = "查询网站查询信息",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(ResearchProjectQuery researchProjectQuery, Integer pageNum, Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<ResearchProjectQuery> list = researchProjectQueryService.selectResearchProjectQueryList(researchProjectQuery);
        return getDataTable(list);
    }

    @ApiOperation(value = "添加网站查询信息",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ResearchProjectQuery researchProjectQuery){
        return toAjax(researchProjectQueryService.insertResearchProjectQuery(researchProjectQuery));
    }

    @ApiOperation(value = "修改网站查询信息",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ResearchProjectQuery researchProjectQuery){
        return toAjax(researchProjectQueryService.updateResearchProjectQuery(researchProjectQuery));
    }

    @ApiOperation(value = "删除网站查询信息",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        //先查询该网站有没有用户密码
        Integer password = researchProjectQueryService.selectResearchProjectQueryPasswordByQueryId(id);
        if(password>0){
            return AjaxResult.warn("该网站有用户密码，请先删除用户密码");
        }
        return toAjax(researchProjectQueryService.deleteResearchProjectQueryById(id));
    }
}
